// Fill out your copyright notice in the Description page of Project Settings.

#include "Isalong2.h"
#include "CIsAlongGameMode.h"
#include "CPlayerCharacter.h"
#include "UIManager.h"
#include "ZombieItem.h"
#include "RootZombieBase.h"
#include "CPlayerCharacterController.h"

ACIsAlongGameMode* ACIsAlongGameMode::_isAlongGameMode = nullptr;

void ACIsAlongGameMode::SpawnItem(int32 id, int32 count, int32 addition)
{
	ACPlayerCharacter* Character= Cast<ACPlayerCharacter>(ACPlayerCharacterController::_localPlayerController->GetPawn());
	if (Character!=nullptr)
	{
		AInteractItem::ConstructTookUpItemByID(Character, id, count, Character->GetActorLocation() + Character->GetActorForwardVector() * 100 + Character->GetActorUpVector() * 25, addition);
	}
}

void ACIsAlongGameMode::SpawnItemInTimes(int32 id, int32 count, int32 addition)
{
	for (int i=0;i<count;i++)
	{
		ACPlayerCharacter* Character = Cast<ACPlayerCharacter>(ACPlayerCharacterController::_localPlayerController->GetPawn());
		if (Character != nullptr)
		{
			AInteractItem::ConstructTookUpItemByID(Character, id, 1, Character->GetActorLocation() + Character->GetActorForwardVector() * 100 + Character->GetActorUpVector() * 25, addition);
		}
	}
}

void ACIsAlongGameMode::SpawnZombieAtLocation(FName name, FVector location)
{
	UDataTable* dt = LoadObject<UDataTable>(NULL, ResourceManager::_resourceManager->_zombieDTUrl);
	FZombieItem* zbit= dt->FindRow<FZombieItem>(name, "");
	FTransform tf;
	tf.SetLocation(location);
	FActorSpawnParameters ActorSpawnParams;
	ActorSpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButDontSpawnIfColliding;
	ARootZombieBase* zb = GetWorld()->SpawnActor<ARootZombieBase>(ARootZombieBase::StaticClass(), tf,ActorSpawnParams);
	if (zb == nullptr) return;
	zb->SpawnDefaultController();
	zb->InitInfo(zbit->SKMesh, zbit->AnimBP);
}

void ACIsAlongGameMode::SpawnZombie(FName name)
{
	ACPlayerCharacter* Character = Cast<ACPlayerCharacter>(ACPlayerCharacterController::_localPlayerController->GetPawn());
	FVector direction= Character->GetActorForwardVector().RotateAngleAxis(FMath::RandRange(-1.047,1.047),Character->GetActorUpVector());
	FVector location = Character->GetActorLocation() + direction * 400;
	SpawnZombieAtLocation(name, location);
}

void ACIsAlongGameMode::SpawnZombieInTimes(FName name, int32 count)
{
	while (count > 0)
	{
		SpawnZombie(name);
		count--;
	}
}

ACIsAlongGameMode::ACIsAlongGameMode() :Super()
{
	//set pawn class

	ACGameManager::_gameManager->WriteLine("GameMode_Construct");

	DefaultPawnClass = ACPlayerCharacter::StaticClass();
	PlayerControllerClass = ACPlayerCharacterController::StaticClass();
	HUDClass = AUIManager::StaticClass();

	_isAlongGameMode = this;

	GlobalData::CleanThis();
	GlobalData::Init();
	ResourceManager::Init();
}


